// 引入mock 
import Mock from 'mockjs'
let List = []; // 返回的数据 相当于后台数据库
for (let i = 0; i < 100; i++) {
    List.push({
        id: i,
        name: Mock.Random.cname(), // 随机成中文名
        age: Mock.Random.integer(0, 10),//级别
        department: ['政治部','指挥部','保障部','治安大队','法制大队','出入境大队','派出所','交警大队'][Math.floor(Math.random()*['政治部','指挥部','保障部','治安大队','法制大队','出入境大队','派出所','交警大队'].length)],//部门
        major: ['民警','公安','巡特警','辅警','特警'][Math.floor(Math.random()*['民警','公安','巡特警','辅警','特警'].length)],//职务

        //分数是小分加的总和
        grade: [
            {   
                a:1,
                b:"加分详情1",
                c:Mock.Random.integer(0, 10),
            },{
                a:2,
                b:"加分详情2",
                c:Mock.Random.integer(0, 10),
            }
            
        ],//小分数
        grades: Mock.Random.integer(0, 100),//总和分数

        address: Mock.Random.county(true),
        pay: Mock.Random.integer(0, 1),
        graduateTime: Mock.Random.date('yyyy-MM-dd')
    })
}


export default {
    // 获取所有的用户接口
    userMock: (options) => {
        let { searchvalue} = JSON.parse(options.body) // 获取参数
        // console.log(searchvalue, page, size);
        //第一步：根据search 返回对应的数据
        let newlist = List.filter((item) => {
            // return item.name.indexOf(searchvalue) != -1
            return item.name.indexOf(searchvalue) != -1
            
        })
        // console.log(List);

        // 第二步：根据page 和size  返回对应的最终数据
        // 将newlist的前10条数据返回  arr.slice(起始下标，结束下标)
        // 1  0-10    （1-1）*10 - 1*10  =>  ((page-1)*size,page*size)
        // 2  10-20   （2-1）*10 - 2*10
        // 3  20- 30  （3-1）*10 - 3*10
        // let newlist1 = newlist.slice((page - 1) * size, page * size)
        console.log(newlist);
        
        return {
            code: 200,
            msg: "请求成功",
            datas: newlist,
            // datas: List,
            totalpage: List.length, // 数据总条数
            // size: size,
            // currentPage: page
        }

    },
    deleteMock: (options) => {
        //console.log(options)
        // 获取参数
        let id = options.url.split('?')[1].split('=')[1]
        console.log(id)
        let list1 = List.filter((item) => {
            return item.id != id
        })
        List = list1
        return {
            code: 200,
            msg: "删除成功",
            data: null
        }
    },
    editMock: (options) => {
        console.log(JSON.parse(options.body))
        let { address,
            age,
            graduateTime,
            id,
            department,
            grade,
            grades,
            major,
            name,
            pay } = JSON.parse(options.body)

        List.forEach((item) => {
            if (item.id == id) {
                item.age = age
                item.department = department
                item.grade = grade
                item.grades = grades
                item.graduateTime = graduateTime
                item.major = major
                item.name = name
                item.pay = pay
                item.address = address
            }
        })
        return {
            code: 200,
            msg: "编辑成功",
            data: null
        }
    },
    addMock: (options) => {
        let { address,
            age,
            department,
            grade,
            grades,
            graduateTime,
            major,
            name,
            pay } = JSON.parse(options.body)
        List.unshift({
            id: List[0].id - 1,
            address,
            age,
            department,
            grade,
            grades,
            graduateTime,
            major,
            name,
            pay
        })
        return {
            code: 200,
            msg: "添加成功",
            data: null
        }
    }
}